package com.xrh.core.generater.mongo;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.xrh.core.generater.bean.UserInput;
import com.xrh.core.generater.interfaces.TemplateRootPath;
import com.xrh.core.generater.utils.FileUtils;
import com.xrh.core.generater.utils.MarkEnum;
import com.xrh.core.generater.utils.TemplateNameEnum;
import com.xrh.core.generater.utils.TemplateReader;
import com.xrh.core.generater.utils.JavaFileAttributeUtils.Attribute;
import com.xrh.core.generater.utils.JavaFileAttributeUtils.MongoEntityInfo;

//QueryRequest文件生成器
public class QueryRequestGenerater {

		//生成文件
		public static void generateFile(UserInput userInput , MongoEntityInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt = generateTemplateTxt(userInput, tableInfo, templateRootPath);
			
			FileUtils.generateFile(userInput.getOutputDirPath() + File.separator + "request" + File.separator + "queryRequest" , tableInfo.getBigClassName() + "QueryRequest.java", templateTxt);
	
		}
		
		/**
		* @function 生成模板字符串
		* @author 肖荣辉
		* @date 2018年10月23日
		*/
		public static String generateTemplateTxt(UserInput userInput , MongoEntityInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt =  TemplateReader.readTextTemplate(templateRootPath , TemplateNameEnum.QueryRequest模板.name());
			
			//通用替换
			templateTxt = MarkEnum.replaceMark(templateTxt , userInput , tableInfo);
			
			//私有属性文本
			templateTxt = MarkEnum.replace(MarkEnum.PRIVATE_PROPERTY_MARK , templateTxt , generatePrivatePropertyTxt(tableInfo));
			
			//createQuery方法查询的文本
			templateTxt = MarkEnum.replace(MarkEnum.MONGO_QUERY_TXT_MARK , templateTxt , generateQueryTxt(tableInfo));
	
			
			return templateTxt;
		}
		

		//生成私有属性文本
		private static String generatePrivatePropertyTxt(MongoEntityInfo tableInfo) {
			
			List<Attribute> attributeList = 	tableInfo.getAttributeList();
			
			StringBuilder sb = new StringBuilder();
			
			 String tab1 = "\n\t";
			 
			 String txt = 	tab1 + "/**"+MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name()+" */"+
		 				MarkEnum.API_MODEL_PROPERTY_MARK.name() +
		 				tab1 + "private "+MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name()+ " "+MarkEnum.PROPERTY_NAME_MARK.name()+";";
			  
			 for(Attribute item : attributeList){
				
				  
				  /*日期特殊处理*/
				  if(item.isDate()) {
					  
					  sb.append(txt.replaceAll(MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name(), item.getComment())
					  			.replaceAll(MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name(), item.getType())
					  			.replaceAll(MarkEnum.API_MODEL_PROPERTY_MARK.name(), generateAnnotationTxt(tab1, item.getComment()))
					  			.replaceAll(MarkEnum.PROPERTY_NAME_MARK.name(), item.getName() + "Start")).append("\n");
					  
					  sb.append(txt.replaceAll(MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name(), item.getComment())
							  .replaceAll(MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name(), item.getType())
							  .replaceAll(MarkEnum.API_MODEL_PROPERTY_MARK.name(), generateAnnotationTxt(tab1, item.getComment()))
							  .replaceAll(MarkEnum.PROPERTY_NAME_MARK.name(), item.getName() + "End")).append("\n");
				  }else {
					  sb.append(txt.replaceAll(MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name(), item.getComment())
					  			.replaceAll(MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name(), item.getType())
					  			.replaceAll(MarkEnum.API_MODEL_PROPERTY_MARK.name(), generateAnnotationTxt(tab1, item.getComment()))
					  			.replaceAll(MarkEnum.PROPERTY_NAME_MARK.name(), item.getName())).append("\n");
				  }
				  
			}
			
			return sb.toString().trim();
			
		}
		
		/**
		 * @function 生成注解标记
		 * @author 肖荣辉
		 * @date 2021年6月29日
		*/
		private static String generateAnnotationTxt(String tab , String comment) {
			
			String txt = "";
			
			txt+= tab + "@ApiModelProperty(\""+comment+"\")";
			
			return txt;
		}
		
		/**
		 * @function 生成查询if判断txt
		 * @author 肖荣辉
		 * @date 2021年6月29日
		 */
		private static String generateQueryTxt(MongoEntityInfo tableInfo) {
			
			List<Attribute> attributeList = 	tableInfo.getAttributeList();
			
			String txt = "";
			
			 for(Attribute item : attributeList){
				 
				 String propertyName = item.getName();
				
				if(item.isString()) {
					txt += "\t\tif(StringUtils.isNotBlank("+propertyName+"))\n" + 
							    "\t\t\tlist.add(Criteria.where(\""+propertyName+"\").is("+propertyName+"));\n\n";
				}else if(item.isDate()) {
					
					txt += "\t\tif("+propertyName+"Start != null)\n" + 
						     "\t\t\tlist.add(Criteria.where(\""+propertyName+"\").gte("+propertyName+"Start));\n\n";
					
					txt += "\t\tif("+propertyName+"End != null)\n" + 
						    "\t\t\tlist.add(Criteria.where(\""+propertyName+"\").lte("+propertyName+"End));\n\n";
					
				}else {
					txt += "\t\tif("+propertyName+" != null)\n" + 
							 	"\t\t\tlist.add(Criteria.where(\""+propertyName+"\").is("+item.getName()+"));\n\n";
				}
				
			}
			
			return txt;
		}
		
		
}
